import { h, ref, render } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';
import { c } from 'naive-ui';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public auctionId = null; // 抢购
  public productId = null; // 商品
  public basePrice = null; // 起拍价格
  public minBidPrice = null; // 最低出价
  public startTime = null; // 当天开始时间
  public endTime = null; // 当天结束时间
  public weekday = null; // 每周运行时间
  public count = 0; // 数量
  public status = 0; // 状态

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  auctionId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入抢购',
  },
  productId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入商品',
  },
  startTime: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入当天开始时间',
  },
  endTime: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入当天结束时间',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'weekday',
    component: 'NCheckbox',
    label: '每周运行时间',
    giProps: {
      span: 1,
    },
    componentProps: {
      placeholder: '请选择每周运行时间',
      options: dict.getOption('weekday'),
      onUpdateChecked: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'auctionId',
    component: 'NSelect',
    label: '活动名称',
    componentProps: {
      placeholder: '请输入活动名称',
      options: dict.getOption('auctionOption'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'productId',
    component: 'NSelect',
    label: '商品名称',
    componentProps: {
      placeholder: '请输入商品名称',
      options: dict.getOption('productOption'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  // {
  //   title: 'id',
  //   key: 'id',
  //   align: 'left',
  //   width: -1,
  // },
  {
    title: '抢购',
    key: 'auctionId',
    align: 'left',
    width: 80,
    render(row: State) {
      return renderOptionTag('auctionOption', row.auctionId);
    },
  },
  {
    title: '商品',
    key: 'productId',
    align: 'left',
    width: 250,
    render(row: State) {
      return renderOptionTag('productOption', row.productId);
    },
  },
  {
    title: '起拍价格',
    key: 'basePrice',
    align: 'left',
    width: 80,
  },
  {
    title: '最低出价',
    key: 'minBidPrice',
    align: 'left',
    width: 80,
  },
  {
    title: '开始时间',
    key: 'startTime',
    align: 'left',
    width: 100,
  },
  {
    title: '结束时间',
    key: 'endTime',
    align: 'left',
    width: 100,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('auctionStatus', row.status);
    },
  },
  {
    title: '每周运行时间',
    key: 'weekday',
    align: 'left',
    width: -1,
    render(row: State) {
      // 创建一个 span 元素作为容器，并初始化 children 为空数组
      const node = h('span', { class: 'weekday-container' }, []);

      // 遍历 row.weekday 数组
      row.weekday.forEach((item) => {
        // 获取对应的标签文本
        const label = renderOptionTag('weekday', item);

        // 创建一个 span 元素来显示标签文本，并将其添加到 node 的 children 中
        node.children.push(h('span', { class: 'weekday-item' }, label));
      });

      // 返回最终的节点
      return node;
    },
  },
  {
    title: '数量',
    key: 'count',
    align: 'left',
    width: 80,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['auctionOption', 'productOption', 'weekday', 'auctionStatus']);
}
